#include<iostream>
#include<algorithm>
#include<string> 
using namespace std;
typedef long long LL;
string s;
const int N = 5e5 + 10;
int a[N];
int cnt, x, y;
int main() {
	int t;
	cin >> t;
	while (t--) {
		cnt = 0;
		getchar();
		getline(cin, s);
		//cout<<s<<endl;
		//getchar();
		cin >> x >> y;
		int temp = -1;
		bool flag = 0;
		for (int i = 0; i < s.size(); i++) {
			if (s[i] >= '0' && s[i] <= '9') {
				if (!flag)temp = 0, flag = 1;
				temp = temp * 10 + s[i] - '0';
			}
			else {
				if (temp == -1)continue;
				a[++cnt] = temp;
				temp = -1;
				flag = 0;
			}
		}
		//		int x;
		//		while(cin>>x){
		//			a[++cnt]=x;
		//		}
		if (y == 1) {
			cout << "[";
			for (int i = 1; i <= cnt; i++) {
				if (i != cnt) {
					cout << "[";
					cout << a[i];
					cout << "],";
				}
				else {
					cout << "[";
					cout << a[i];
					cout << "]";
				}
			}
			cout << "]" << endl;
			continue;
		}
		int cnt1 = 1;
		//cout<<"cnt=="<<cnt<<endl;
		//cout<<"x=="<<x<<endl;
		//cout<<"y=="<<y<<endl;
		int xx = 1;
		cout << "[";
		for (int i = 1; i <= cnt; i++) {
			if (cnt1 != y) {
				if (cnt1 == 1) {
					cout << "[";
				}
				cout << a[i] << ", ";
				cnt1++;
			}
			else if (cnt1 == y) {
				cout << a[i] << "]";
				cnt1 = 1;
			}
			if (i % y == 0) {
				//cout<<"i=="<<i<<endl;
				if (i != cnt) {
					cout << ", ";
				}
			}
			xx++;
		}
		cout << "]" << endl;
	}

	return 0;
}